package org.boge.sharpdoc.controller;

import com.github.pagehelper.PageInfo;
import org.boge.sharpdoc.bean.Administrator;
import org.boge.sharpdoc.bean.LayuiReturnData;
import org.boge.sharpdoc.config.Settings;
import org.boge.sharpdoc.service.AdministratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.UUID;

@Controller
@RequestMapping(value = "admin")
public class AdministratorController {
    @Autowired
    private HttpSession session;
    @Autowired
    private AdministratorService administratorService;
    @Autowired
    private Settings settings;

    @ResponseBody
    @PostMapping("changePassword")
    public LayuiReturnData changePassword(String oldPassword, String newPassword) {
        Administrator current_administrator = (Administrator) session.getAttribute("current_administrator");
        current_administrator.setPassword(oldPassword);
        if (administratorService.login(current_administrator) != null) {
            current_administrator.setPassword(newPassword);
            boolean ok = administratorService.changePassword(current_administrator);
            if (ok)
                return new LayuiReturnData(0, true);
            else
                return new LayuiReturnData(0, false, "密码更改错误，请输入合法的密码！");
        }
        return new LayuiReturnData(0, false, "原始密码错误！");
    }

    @PostMapping("setInfo")
    @ResponseBody
    public LayuiReturnData setInfo(Administrator administrator) {
        Administrator current_administrator = (Administrator) session.getAttribute("current_administrator");
        administrator.setAdminid(current_administrator.getAdminid());
        administrator.setStatus(current_administrator.getStatus());
        boolean t = administratorService.updateInfo(administrator);
        if (t) {
            session.setAttribute("current_administrator", administrator);
            return new LayuiReturnData(0, t);
        }
        return new LayuiReturnData(0, t);
    }

    @PostMapping("uploadAvatar")
    @ResponseBody
    public LayuiReturnData uploadAvatar(MultipartFile file, HttpServletRequest request) throws IOException {
        String suffix = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().indexOf('.'));
        String filename = UUID.randomUUID() + suffix;
        File storedFile = new File(settings.getAvatarsDir(), filename);
        file.transferTo(storedFile);
        Administrator administrator = (Administrator) session.getAttribute("current_administrator");
        administrator.setAvatar(filename);
        administratorService.updateInfo(administrator);
        String url = "/picture/getAvatar?fileName=" + filename;
        return new LayuiReturnData(0, true, url);
    }

    @PostMapping("getAllAdmin")
    @ResponseBody
    public LayuiReturnData getAllAdmin(Integer index, Integer pageSize) {
        PageInfo<Administrator> pageInfo = administratorService.getAllAdminByPage(index, pageSize);
        return new LayuiReturnData(0, true, "数据查询成功！", (int) pageInfo.getTotal(), pageInfo.getList());
    }

    @GetMapping("setInfoPage")
    public String setInfoPage() {
        return "admin/user/setInfo";
    }

    @GetMapping("forgetPage")
    public String forgetPage() {
        return "admin/user/forget";
    }

    @GetMapping("loginPage")
    public String loginPage() {
        return "admin/user/login";
    }

    @GetMapping("setPasswordPage")
    public String setPasswordPage() {
        return "admin/user/setPassword";
    }

    @GetMapping("manageAdministratorPage")
    public String manageAdministratorPage() {
        return "admin/user/manage";
    }

    @ResponseBody
    @PostMapping("login")
    public LayuiReturnData login(Administrator administrator, String inputCode) {
        String checkCodeImg = (String) session.getAttribute("checkCodeImg");
        if (inputCode.equalsIgnoreCase(checkCodeImg)) {
            Administrator administrator_db = administratorService.login(administrator);
            if (administrator_db == null)
                return new LayuiReturnData(0, false, "用户名或密码错误！");
            else if (administrator_db.getStatus().equals("禁用")) {
                return new LayuiReturnData(0, false, "该账户已被禁用！");
            } else {
                session.setAttribute("current_administrator", administrator_db);
                return new LayuiReturnData(0, true);
            }
        }
        return new LayuiReturnData(0, false, "验证码错误！");
    }

    @GetMapping("logout")
    public String logout() {
        session.invalidate();
        return "redirect:/admin";
    }

    @PostMapping("addAdmin")
    @ResponseBody
    public LayuiReturnData addAdmin(Administrator administrator) {
        administratorService.addAdmin(administrator);
        return new LayuiReturnData(0, true, "新添加的管理员编号为：" + administrator.getAdminid());
    }

    @PostMapping("changeStatus")
    @ResponseBody
    public LayuiReturnData changeAdministratorStatus(Integer adminid, String status) {
        administratorService.changeStatus(adminid, status);
        return new LayuiReturnData(0, true, "状态更改成功！");
    }
}